<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('项目成员管理')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">项目信息</h3>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-sm-6">
                                <label>项目名称：</label>
                                <span th:text="${project.name}"></span>
                            </div>
                            <div class="col-sm-6">
                                <label>客户名称：</label>
                                <span th:text="${project.customerName}"></span>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-6">
                                <label>项目主页：</label>
                                <span th:text="${project.homepage}"></span>
                            </div>
                            <div class="col-sm-6">
                                <label>项目状态：</label>
                                <span th:if="${project.state == 1}" class="label label-success">启用</span>
                                <span th:if="${project.state == 0}" class="label label-danger">停用</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>用户名称：</label>
                                <input type="text" name="userName"/>
                            </li>
                            <li>
                                <label>角色：</label>
                                <select name="roleId" id="roleId">
                                    <option value="">所有</option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="addMember()" shiro:hasPermission="pm:project:member:add">
                    <i class="fa fa-plus"></i> 添加成员
                </a>
                <a class="btn btn-danger" onclick="$.operate.removeAll()" shiro:hasPermission="pm:project:member:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: layout-latest-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script th:inline="javascript">
        var projectId = [[${project.id}]];
        var detailFlag = [[${@permission.hasPermi('pm:project:member:detail')}]];
        var listFlag = [[${@permission.hasPermi('pm:project:member:list')}]];
        var editFlag = [[${@permission.hasPermi('pm:project:member:edit')}]];
        var removeFlag = [[${@permission.hasPermi('pm:project:member:remove')}]];
        var prefix = ctx + "project/member";

        $(function() {
            // 加载角色列表
            loadRoleList();
            
            var options = {
                url: ctx + "project/member/list/" + projectId,
                removeUrl: ctx + "project/member/removeFromProject",
                modalName: "项目成员",
                columns: [{
                    checkbox: true,
                    visible: removeFlag != 'hidden'
                },
                {
                    field: 'id',
                    title: '主键ID',
                    visible: false
                },
                {
                    field: 'userName',
                    title: '用户名称'
                },
                {
                    field: 'roleName',
                    title: '角色名称'
                },
                {
                    field: 'createTime',
                    title: '加入时间',
                    formatter: function(value, row, index) {
                        return $.common.dateFormat(value);
                    }
                },
                {
                    field: 'createBy',
                    title: '添加人'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-warning btn-xs ' + detailFlag + '" href="javascript:void(0)" onclick="$.operate.detail(\'' + row.id + '\')"><i class="fa fa-search"></i> 详细</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="editMember(\'' + row.id + '\')"><i class="fa fa-edit"></i> 编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i> 删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        // 加载角色列表
        function loadRoleList() {
            $.ajax({
                url: ctx + "project/role/list",
                type: "GET",
                success: function(result) {
                    if (result.code == 0) {
                        var roleSelect = $('#roleId');
                        $.each(result.data, function(index, role) {
                            if (role.status == '0') { // 只显示正常状态的角色
                                roleSelect.append('<option value="' + role.id + '">' + role.name + '</option>');
                            }
                        });
                    }
                }
            });
        }

        // 添加成员
        function addMember() {
            var url = ctx + "project/member/add?projectId=" + projectId;
            $.modal.open("添加成员", url);
        }

        // 编辑成员
        function editMember(id) {
            var url = ctx + "project/member/edit/" + id;
            $.modal.open("编辑成员", url);
        }

        // 删除成员
        function removeMember(id) {
            $.modal.confirm("确认要删除该成员吗？", function() {
                $.operate.post(prefix + "/remove", { "ids": id });
            });
        }
    </script>
</body>
</html>